import axios from "axios";
import NProgress from "nprogress";
import { useUserInfoStore } from "@/store/userInfo";

const userInfo = useUserInfoStore()
const req = axios.create({
    baseURL: "/api",
    timeout: 5000,
})

req.interceptors.request.use(function(config) {
    NProgress.start()
    config.headers["ContentType"] = "application/json;charset=UTF-8"
    config.headers["Authorization"] = userInfo.token
    return config
}, function(error) {
    NProgress.done()
    return Promise.reject(error)
})

req.interceptors.response.use(function (response) {
    NProgress.done();
    return response;
  }, function (error) {
    NProgress.done();
    console.error(error)
    return Promise.reject(error);
  });

// 默认带Token的JSON格式请求
export default req



const validateEmailReq = axios.create({
    baseURL: "/api",
    timeout: 5000,
})

validateEmailReq.interceptors.request.use(function(config) {
    NProgress.start()
    config.headers["ContentType"] = "application/json;charset=UTF-8"
    return config
}, function(error) {
    NProgress.done()
    return Promise.reject(error)
})

validateEmailReq.interceptors.response.use(function (response) {
    NProgress.done();
    return response;
  }, function (error) {
    NProgress.done();
    console.error(error)
    return Promise.reject(error);
});

export {validateEmailReq}
